package com.wht.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wht.annotation.RequiresPermissionsDesc;
import com.wht.domain.DtsAdmin;
import com.wht.service.DtsAdminService;
import com.wht.util.AdminResponseCode;
import com.wht.util.AdminResponseUtil;
import com.wht.util.AuthSupport;
import com.wht.util.ResponseUtil;
import com.wht.util.bcrypt.BCrypt;
import com.wht.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wanghaotian
 * time:2022/12/08,0008 15:16
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/admin")
public class AdminAdminController {

    private static final Logger logger = LoggerFactory.getLogger(AdminAdminController.class);

    @Autowired
    private DtsAdminService adminService;

    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "查询")
    @GetMapping("/list")
    public Object list(String username,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit,
                        @RequestParam(defaultValue = "add_time") String sort,
                        @RequestParam(defaultValue = "desc") String order
                        ){
        logger.info("【请求开始】操作人:[\" + AuthSupport.userName()+ \"] 系统管理->管理员管理->查询,请求参数:username:{},page:{}",username,page);
        IPage<DtsAdmin> iPage = adminService.querySelective(username,page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsAdmin> adminList = iPage.getRecords();
        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", adminList);
        logger.info("【请求结束】系统管理->管理员管理->查询,响应结果:{}", JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->管理员管理->添加,请求参数:{}",JSONObject.toJSONString(admin));
        String username = admin.getUsername();
        List<DtsAdmin> adminList = adminService.findAdmin(username);
        if(adminList.size() > 0){
            logger.error("系统管理->管理员管理->添加 ,错误：{}", AdminResponseCode.ADMIN_NAME_EXIST.desc());
            return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
        }
        String password = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        admin.setPassword(encode);
        adminService.add(admin);
        logger.info("【请求结束】系统管理->管理员管理->添加,响应结果:{}",JSONObject.toJSONString(admin));
        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员管理"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->管理员管理->编辑,请求参数:{}",JSONObject.toJSONString(admin));
        Integer anotherAdminId = admin.getId();
        if (anotherAdminId == null) {
            return ResponseUtil.badArgument();
        }
        String password = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        admin.setPassword(encode);
        if (adminService.updateById(admin) == 0) {
            logger.error("系统管理->管理员管理-编辑 ,错误：{}", "更新数据失败！");
        }
        logger.info("【请求结束】系统管理->管理员管理->编辑,响应结果:{}",JSONObject.toJSONString(admin));
        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->管理员管理->删除,请求参数:{}",JSONObject.toJSONString(admin));
        Integer id = admin.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        adminService.deleteById(id);
        logger.info("【请求结束】系统管理->管理员管理->删除 成功！");
        return ResponseUtil.ok();
    }

}
